package org.yxing.palpalgps.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.oauth.OAuth;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class InterestSelectTwoActivity extends AbstractActivity implements OnItemClickListener{
    private Adapter mAdapter;
    
    private String iid;
    
    private ListView mListView;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.interest_select_two);
        setTitle(getString(R.string.select_interest));
        iid = getIntent().getStringExtra("iid");
        
        mListView = (ListView) findViewById(android.R.id.list);
        mListView.setOnItemClickListener(this);
        
        mAdapter = new Adapter();
        mListView.setAdapter(mAdapter);
        
        mAdapter.getData();
    }
    
    private class Adapter extends BaseAdapter {
        
        ArrayList<JSONObject> data;
        
        public Adapter() {
            data = new ArrayList<JSONObject>();
        }

        public int getCount() {
            return data.size();
        }

        public JSONObject getItem(int position) {
            return data.get(position);
        }

        public long getItemId(int position) {
            long id = 0;
            try {
                id = getItem(position).getInt("id");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return id;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.interest_select_two_item, null);
            TextView nameView = (TextView) convertView.findViewById(R.id.name);
            JSONObject object = getItem(position);
            try {
                nameView.setText(object.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }
        
        public void setItem(JSONObject obj) {
            data.add(obj);
        }
        
        public void getData() {
            final List<BasicNameValuePair> params = OAuth.createAuthParams();
            params.add(new BasicNameValuePair("pid", iid));
            AsyncRequest ar = new AsyncRequest(InterestSelectTwoActivity.this);
            
            ar.fetch("group/list", params, new AsyncRequest.FetchListener() {
                
                public void onError(int code, String msg) {
                    
                }
                
                public void onComplete(JSONObject response) {
                    try {
                        JSONArray results = response.getJSONArray("result");
                        for (int i = 0; i < results.length(); i ++) {
                            setItem(results.getJSONObject(i));
                        }
                        notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    /**
     * add interest
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
        Dialog dialog = new AlertDialog.Builder(this)
        .setTitle(getString(R.string.are_you_sure_add))
        .setIcon(getResources().getDrawable(android.R.drawable.ic_menu_help))
        .setPositiveButton((R.string.ok), new Dialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                List<BasicNameValuePair> params = OAuth.createAuthParams();
                params.add(new BasicNameValuePair("iid", Long.toString(id)));
                AsyncRequest ar = new AsyncRequest(InterestSelectTwoActivity.this);
                ar.fetch("group/join", params, new FetchListener() {
                    
                    public void onError(int code, String msg) {
                        displayToast(msg);
                    }
                    
                    public void onComplete(JSONObject response) {
                        displayToast(getString(R.string.toast_add_success));
                    }
                });
            }
        })
        .setNegativeButton(getString(R.string.cancel), null)
        .create();
    
        dialog.show();
    }
}
